package com.spudtech
{
    
    import flash.geom.*;
    import flash.display.*;
    
    
    public class SpudGroup extends SpudObject
    {
        
        protected var _members : Array;
        
        protected var _scene : SpudScene;
        
        
        public function SpudGroup(init_members : Array = null)
        {
            super();
            
            _scene = null;
            
            _members = new Array();
            
            if (init_members != null)
            {
                // Filter to make sure we only
                // have SpudObjects as children
                
                var is_spud_object : Function = function(item : *, index : int, array : Array) : Boolean
                {
                    if (item is SpudObject)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                
                for each (var obj : SpudObject in init_members.filter(is_spud_object))
                {
                    addChild(obj);
                }
                
            }
        }
        
        public function get scene() : SpudScene
        {
            return _scene;
        }
        public function set scene(new_scene : SpudScene) : void
        {
            if (_scene == new_scene)
            {
                // What? We're already in that scene!
                // Just return
                return;
            }
            
            _scene = new_scene;
            
            for each (var obj : SpudObject in members)
            {
                // Set each member's scene
                
                if (obj is SpudGroup)
                {
                    SpudGroup(obj).scene = this.scene;
                }
                else if (obj is SpudEntity)
                {
                    SpudEntity(obj).scene = this.scene;
                }
            }
            
        }
        
        public function get members() : Array
        {
            return _members;
        }
        
        override public function addChild(obj : DisplayObject) : DisplayObject
        {
            if (!(obj is SpudObject))
            {
                // We can only add SpudObject's to the group
                
                throw new TypeError("Method requires a SpudObject");
                
            }
            else
            {
                members.push(SpudObject(obj));
                
                super.addChild(obj);
                
                if (!(SpudObject(obj).alive))
                {
                    SpudObject(obj).begin();
                }
                
                
                // Set the new member's scene
                
                if (obj is SpudGroup)
                {
                    SpudGroup(obj).scene = this.scene;
                }
                else if (obj is SpudEntity)
                {
                    SpudEntity(obj).scene = this.scene;
                }
                
                return obj;
                
            }
        }
        
        override public function removeChild(child : DisplayObject) : DisplayObject
        {
            var is_not_child : Function = function(item : *, index : int, array : Array) : Boolean
            {
                if (item == child)
                {
                    // That's the one we want to get
                    // rid of, so return false
                    return false;
                }
                else
                {
                    // Otherwise, we return true
                    return true;
                }
            }
            
            _members = _members.filter(is_not_child);
            
            super.removeChild(child);
            
            return child;
            
        }
        
        
        override public function begin() : void
        {
            super.begin();
        }
        
        override public function destroy() : void
        {
            super.destroy();
        }
        
        
        override public function render(offset : Matrix,
                                        clip_rect : Rectangle,
                                        draw_graphics : Graphics) : void
        {
            var to_draw_offset : Matrix = offset.clone();
            
            for each (var member : SpudObject in members)
            {
                member.render(to_draw_offset,clip_rect,draw_graphics);
            }
        }
        
        
        override public function update() : void
        {
            super.update();
            
            if (!(SpudRoot.paused))
            {
                // If we're paused, nothing moves
                
                for each (var obj : SpudObject in members)
                {
                    obj.update();
                }
            }
            
        }
        
    }
    
}
